<?php
/**
 * Created by PhpStorm.
 * User: 我想有个家
 * Date: 2019/8/28
 * Time: 9:11
 */

namespace App\Lib;


use Hyperf\Contract\ConfigInterface;

class AES128 {
    private $cipher = "AES-128-CBC";
    // 必须16位，如果不是有可能导致和APP加密结果不一致
    private $key;
    // 必须16位，如果不是有可能导致和APP加密结果不一致
    private $iv;

    public function __construct(ConfigInterface $config) {
        $conf = $config->get('md.token');
        $this->key = "{$conf['key']}es";
        $this->iv = "{$conf['iv']}es";
    }

    /**
     * 加密
     * @param $content string
     * @return string
     */
    public function encrypt($content) {
        return base64_encode(openssl_encrypt($content, $this->cipher, $this->key, 1, $this->iv));
    }

    /**
     * 解密
     * @param $content string
     * @return string
     */
    public function decrypt($content) {
        return openssl_decrypt(base64_decode($content), $this->cipher, $this->key, 1, $this->iv);
    }
}
